package com.shigen.demo.pattern.singleton;

/**
 * 双重检查锁 优点：性能高、节省资源
 * 缺点：不优雅
 *
 * @author shigenfu
 * @date 2022/8/14 11:42 上午
 */
public class DoubleCheckSingleton {

  private static volatile DoubleCheckSingleton instance;

  private DoubleCheckSingleton() {

  }

  public static DoubleCheckSingleton getInstance() {
    // 控制要不要加锁
    if (instance == null) {
      synchronized (DoubleCheckSingleton.class) {
        // 控制要不要创建对象
        if (instance == null) {
          instance = new DoubleCheckSingleton();
          // 存在指令重拍的问题
        }
      }
    }
    return instance;
  }
}
